/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.kason.heat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author 卢清波
 * @date 2021/1/25 13:01
 */
public class HeatClientHandler extends ChannelInboundHandlerAdapter
{
    /**
     * 当客户端链接服务器完成会触发该方法
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush("您好服务端，我是客户端"+ctx.channel().id());
        // ctx.fireChannelActive();
    }

    /**
     * 当接收到服务端消息时触发，通道有读取事件
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("接收到服务端消息："+msg);
        if("idle close".equals(msg))
        {
            System.out.println("服务端关闭");
            ctx.channel().closeFuture();
        }
    }


}
